const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin'); //对html打包
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); //清理
const CopywebpackPlugin = require("copy-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
 const cesiumSource = 'node_modules/cesium/Source';
 const cesiumWorkers = '../Build/Cesium/Workers';
module.exports = {
    context: __dirname,
    entry: {
        app: './src/index.js'
    }, //入口文件
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),  //放入dist文件夹下
        sourcePrefix: ''  //需要在cesium编译 
    },
    amd:
    {
        toUrlUndefinde: true    //允许在cesium中使用webpack-friendly的require用法
    },
    node:{
        fs: 'empty'    //允许使用 node中的fs的模块
    },
    devServer:{
        contentBase: path.join(__dirname,'dist')
    },
    resolve:{
        alias:{
            cesium: path.resolve(__dirname,cesiumSource)
        }
    },
    module: {
        rules: [
            {
                test: require.resolve('jquery'),
                use: [
                    {
                        loader: 'expose-loader',
                        options: '$'
                    },
                    {
                        loader: 'expose-loader',
                        options: 'jQuery'
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [     //支持css
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|gif|jpg|jpeg|svg|xml|json|gltf)$/,   //支持其它文件
                use: ['url-loader']
            },
            {
                test: /\.(csv|tsv)$/,
                use: [
                    'csv-loader'
                ]
            },
            {
                test: /\.xml$/,
                use: [
                    'xml-loader'
                ]
            }

        ]
    },
    plugins: [
        //new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({     //html 模版
            template: 'src/index.html'
        }),
        new CopywebpackPlugin([{from:path.join(cesiumSource,cesiumWorkers),to:'Workers'}]),
        new CopywebpackPlugin([{from:path.join(cesiumSource,'Assets'),to:'Assets'}]),
        new CopywebpackPlugin([{from:path.join(cesiumSource,'Widgets'),to:'Widgets'}]),
        new webpack.DefinePlugin({
            CESIUM_BASE_URL: JSON.stringify('')
        }),
        //new webpack.optimize.UglifyJsPlugin()
    ],
    optimization: {
        minimizer: [new TerserPlugin({
            test: /\.js(\?.*)?$/i,
        })],
        splitChunks: {
          chunks: 'async',
          minSize: 30000,
          maxSize: 0,
          minChunks: 1,
          maxAsyncRequests: 5,
          maxInitialRequests: 3,
          automaticNameDelimiter: '~',
          automaticNameMaxLength: 30,
          name: true,
          cacheGroups: {
            vendors: {
              test: /[\\/]node_modules[\\/]/,
              priority: -10
            },
            default: {
              minChunks: 2,
              priority: -20,
              reuseExistingChunk: true
            }
          }
        }
    }
};